﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data;
using System.Data.SqlClient;
using DBLayer;

namespace BALayer
{
    public class HangHoa
    {
        private DAL db = null;

        public HangHoa()
        {
            db = new DAL();
        }

        public DataSet DanhSachHangHoa()
        {
            // id, tenhang, dongia
            return db.ExecuteQueryDataSet("uspDanhSachHangHoa",
                CommandType.Text, null);
        }
        
        public DataSet DanhSachHangHienCo()
        {
            // manhaphang, mahang, tenhang, soluongcon, dongia, handung
            return db.ExecuteQueryDataSet("uspChiTietHangHoa", CommandType.StoredProcedure, null);
        }

        public DataSet NhaCungCapCoHangHetHan()
        {
            // id, tenncc
            string str = "select id, tenncc from ChiTietHangHoa A, NhapHang B, NhaCungCap C where ngayhethan>" + DateTime.Now.ToString()
                + " and trangthai=0 and A.manhaphang=B.manhaphang and B.mancc=C.id";

            return db.ExecuteQueryDataSet("uspNCCCoHangHetHan", CommandType.StoredProcedure, null);
        }

        public DataSet DanhSachHangHetHan()
        {
            // id, manhaphang, mancc, tenncc, mahang, tenhang, soluongcon, ngayhethan
            return db.ExecuteQueryDataSet("uspDanhSachHangHetHan", CommandType.StoredProcedure, null);
        }

        public int ThemHangHoa(ref string error,String tenhang)
        {
            int mahanghoa = -1;
            SqlParameter mahang = new SqlParameter("@Mahang", DbType.Int32);
            mahang.Direction = ParameterDirection.Output;

            if(db.ExecuteNonQuery("uspThemHangHoa", CommandType.StoredProcedure, ref error,
                new SqlParameter("@Tenhang", tenhang),
                mahang))
            {
                mahanghoa = Convert.ToInt32(mahang.Value);
            }

            return mahanghoa;
        }

        public bool XoaHangHoa(ref string error, String id)
        {
            return db.ExecuteNonQuery("delete from HangHoa where id='" + id + "'", CommandType.Text, ref error, null);
        }

        public bool CapNhatHangHoa(ref string error,String id,String tenhang)
        {
            return db.ExecuteNonQuery("update HangHoa set tenhang='" + tenhang + "' where id='" + id + "'", CommandType.Text, ref error, null);
        }

        public DataSet ChiTietHangHoa()
        {
            // manhaphang, mahang, tenhang, soluongcon, dongia, handung
            return db.ExecuteQueryDataSet("uspChiTietHangHoa", CommandType.StoredProcedure, null);
        }

        public bool XoaChiTietHangHoa(ref string error, String id)
        {
            return db.ExecuteNonQuery("uspXoaChiTietHangHoa", CommandType.StoredProcedure, ref error,
                new SqlParameter("@Id", id));
        }

        public bool CapNhatChiTietHangHoa(ref string error,String id,int soluongcon,int trangthai)
        {
            return db.ExecuteNonQuery("uspCapNhatChiTietHangHoa", CommandType.StoredProcedure, ref error,
                new SqlParameter("@Id", id),
                new SqlParameter("@Soluongcon", soluongcon),
                new SqlParameter("@Trangthai", trangthai));
        }
    }
}
